package cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject.vo.OrderProjectPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject.vo.OrderProjectRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.order.orderproject.vo.OrderProjectSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.order.orderproject.OrderProjectDO;
import cn.iocoder.yudao.module.erp.service.business.order.orderproject.OrderProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 项目")
@RestController
@RequestMapping("/erp/order-project")
@Validated
public class OrderProjectController {

    @Resource
    private OrderProjectService orderProjectService;

    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('erp:order-project:create')")
    public CommonResult<Long> createOrderProject(@Valid @RequestBody OrderProjectSaveReqVO createReqVO) {
        return success(orderProjectService.createOrderProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('erp:order-project:update')")
    public CommonResult<Boolean> updateOrderProject(@Valid @RequestBody OrderProjectSaveReqVO updateReqVO) {
        orderProjectService.updateOrderProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:order-project:delete')")
    public CommonResult<Boolean> deleteOrderProject(@RequestParam("id") Long id) {
        orderProjectService.deleteOrderProject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:order-project:query')")
    public CommonResult<OrderProjectRespVO> getOrderProject(@RequestParam("id") Long id) {
        OrderProjectDO orderProject = orderProjectService.getOrderProject(id);
        return success(BeanUtils.toBean(orderProject, OrderProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目分页")
    @PreAuthorize("@ss.hasPermission('erp:order-project:query')")
    public CommonResult<PageResult<OrderProjectRespVO>> getOrderProjectPage(@Valid OrderProjectPageReqVO pageReqVO) {
        PageResult<OrderProjectDO> pageResult = orderProjectService.getOrderProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderProjectRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取项目精简列表")
    @PreAuthorize("@ss.hasPermission('erp:order-project:query')")
    public CommonResult<List<OrderProjectRespVO>> getSimpleOrderProjectList() {
        OrderProjectPageReqVO orderProjectPageReqVO = new OrderProjectPageReqVO();
        orderProjectPageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<OrderProjectDO> orderProjectPage = orderProjectService.getOrderProjectPage(orderProjectPageReqVO);
        return success(convertList(orderProjectPage.getList(), item -> new OrderProjectRespVO().setId(item.getId())
                .setName(item.getName())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目 Excel")
    @PreAuthorize("@ss.hasPermission('erp:order-project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderProjectExcel(@Valid OrderProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderProjectDO> list = orderProjectService.getOrderProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目.xls", "数据", OrderProjectRespVO.class,
                        BeanUtils.toBean(list, OrderProjectRespVO.class));
    }

}